﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sunimage.Spjg.BLL;
using Sunimage.Spjg.BLL.Helper;
using Sunimage.Spjg.BLL.SysMgr;
using Sunimage.Spjg.Common;
using Sunimage.Spjg.Web.Helper;

namespace Sunimage.Spjg.Web.UserClient
{
    public partial class Note : System.Web.UI.Page
    {
        NoteBl noteBl = new NoteBl();
        UserBl userBl = new UserBl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            rptNote.DataSource = noteBl.GetAll();
            rptNote.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (GsjUserHelper.Permissions.Split(',').Contains(((int)PermissionEnum.WriteNote).ToString()))
            {
                Common.Note note = new Common.Note();
                note.CreateTime = DateTime.Now;
                note.Title = tbxTitle.Text.Trim();
                note.Content = tbxContent.Text.Trim();
                note.CreatorId = GsjUserHelper.UserId;

                noteBl.Create(note);

                LoadData();
            }
            else
            {
                AlertHelper.ShowError("您没有发布留言的权限！", this.Page);
            }
        }


        protected void pager_PageChanged(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        protected void rptNote_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Literal ltrAuthor = e.Item.FindControl("ltrAuthor") as Literal;
            if (ltrAuthor != null)
            {
                Common.Note note = e.Item.DataItem as Common.Note;
                Common.User user = userBl.GetById(note.CreatorId);
                if (user != null)
                    ltrAuthor.Text = user.NickName;
                else
                    ltrAuthor.Text = AppConstant.DeleteUserNickname;
            }
        }
    }
}